package com.aaa.json.dao;


import com.aaa.json.dao.BaseDao;
import com.aaa.json.dao.DeptDao;
import com.aaa.json.entity.Dept;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: javamvc-mvcup
 * @description:
 * @author: 诸葛龙辰
 * @create: 2023-03-03 21:07
 * @version:
 **/
public class DeptDaoImpl implements DeptDao {
    @Override
    public List<Map> queryByPage1(int start, int pageSize, Dept dept) {
        //定义查询sql where 1=1 恒成立 为拼接sql做准备 最终做称动态sql
        String sql ="select emb_id, emp_name,year,month,salary,loc from tb_emp_salary where 1=1 ";
        //定义List来动态放置查询条件
        List<String> paramList = new ArrayList();
        //判断部门是否为null
        if (dept != null){
            //判断部门名称是否为null
            if (dept.getEmpName()!=null && !"".equals(dept.getEmpName())){
                sql +=" and emp_name like ? ";
                paramList.add("%"+dept.getEmpName()+"%");
            }
            //判断部门工资是否为null
            if (dept.getLoc()!=null && !"".equals(dept.getLoc())){
                sql += " and loc like ? ";
                paramList.add("%"+dept.getLoc()+"%");
            }
        }
        sql +=" limit "+start+","+pageSize;
        System.out.println("execte sql---------------"+sql);
        //paramList.toArray()把结合转数组
        return BaseDao.executQuery(sql,paramList.toArray());
    }

    @Override
    public int queryTotal1(Dept dept) {
        //定义查询sql where 1=1 恒成立 为拼接sql做准备 最终做称动态sql
        String sql ="select count(*) total from tb_emp_salary where 1=1";
        //定义List来动态放置查询条件
        List<String> paramList = new ArrayList();
        //判断部门是否为null
        if (dept != null){
            //判断部门名称是否为null
            if (dept.getEmpName()!=null && ! "".equals(dept.getEmpName())){
                sql +="and emp_name like ? ";
                paramList.add("%"+dept.getEmpName()+"%");
            }
            //判断部门工资是否为null
            if (dept.getLoc()!=null &&!"".equals(dept.getLoc())){
                sql += "and loc like?";
                paramList.add("%"+dept.getLoc()+"%");
            }
        }

        System.out.println("execte sql---------------:"+sql);
        //查询
        List<Map> mapList = BaseDao.executQuery(sql, paramList.toArray());
        //判断查询是否为null
        if (mapList!=null &&mapList.size()>0){
            //total=9 total=3....
            return Integer.valueOf(mapList.get(0).get("total").toString());
        }

        return 0;
    }

    @Override
    public List<Map> queryAll1() {
        return BaseDao.executQuery("select emb_id, emp_name, year,month,salary ,loc from tb_emp_salary");
    }

    @Override
    public List<Map> queryById1(int embId) {
        return BaseDao.executQuery("select emb_id, emp_name, year,month,salary,loc  from tb_emp_salary  where emb_id=?",embId);
    }

    @Override
    public int add1(Dept dept) {
        return  BaseDao.executeUpdate("insert into tb_emp_salary(emp_name,year,month, salary,loc) values(?,?,?,?,?)",
                new Object[]{dept.getEmpName(), dept.getYear(),dept.getMonth(),dept.getSalary(),dept.getLoc()});
    }

    @Override
    public int update1(Dept dept) {
        return BaseDao.executeUpdate("update tb_emp_salary set emp_name=? ,year=? ,month=?,salary=? ,loc=? where emb_id=?",
                new Object[]{dept.getEmpName(),dept.getYear(),dept.getMonth(),dept.getSalary(),dept.getLoc(),dept.getEmbId()});
    }

    @Override
    public int deleteById1(int embId) {
        return BaseDao.executeUpdate("delete from tb_emp_salary where emb_id=?" ,new Object[]{embId});
    }
}
